<script setup>
import { ref, reactive } from 'vue'
// import config from '@/assets/js/config'
import { showMsg, showConfirm } from '@/assets/js/utils'
import { getCarNumberList, deleteCarNubmer } from '@/assets/api/safe'
import { useRouter } from 'vue-router'
const router = useRouter()

const filterForm = reactive({
    car_plate: '',
})
const currentPage = ref(1)
const pageSize = 10
const total = ref(0)
const dataList = ref([])
function getData(page = 1) {
    const params = Object.assign(
        {
            page,
            limit: pageSize,
        },
        filterForm
    )
    getCarNumberList(params).then((res) => {
        const { Items, totalSize } = res.data
        dataList.value = Items
        currentPage.value = +res.data.currentPage
        total.value = totalSize
    })
}
getData(1)
function handleCurrentChange(e) {
    getData(e)
}
function searchAction() {
    getData(1)
}

// 新建
function newAction() {
    router.push(`/safe/carGate/plate/new`)
}
// 编辑
function editAction(item) {
    router.push(
        `/safe/carGate/plate/new?id=${item.id}&number=${item.car_plate}`
    )
}
// 删除
function deleteAction(item) {
    showConfirm({
        content: '您确定要删除该数据吗',
        onConfirm: () => {
            deleteCarNubmer({ id: item.id }).then(() => {
                showMsg('删除成功')
                getData(currentPage.value)
            })
        },
    })
}
</script>
<template>
    <div class="filter-box white-box d-flex">
        <el-form :inline="true" :model="filterForm" class="d-flex-1">
            <el-form-item label="车牌">
                <el-input v-model="filterForm.car_plate" placeholder="请输入" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="searchAction">搜索</el-button>
            </el-form-item>
        </el-form>
        <div class="el-form-item">
            <el-button type="primary" @click="newAction">新增</el-button>
        </div>
    </div>
    <div class="white-box mt-20 d-flex-1 overflow-auto">
        <el-table :data="dataList" class="my-10 full">
            <el-table-column label="车牌号" prop="car_plate" />
            <el-table-column label="创建时间" prop="create_time" />
            <el-table-column label="操作" width="150">
                <template #default="scope">
                    <el-tooltip effect="dark" content="编辑" placement="top">
                        <el-button
                            type="primary"
                            link
                            @click="editAction(scope.row)"
                        >
                            <el-icon size="16"><Edit /></el-icon>
                        </el-button>
                    </el-tooltip>
                    <el-tooltip effect="dark" content="删除" placement="top">
                        <el-button
                            type="primary"
                            link
                            @click="deleteAction(scope.row)"
                        >
                            <el-icon size="16"><Delete /></el-icon>
                        </el-button>
                    </el-tooltip>
                </template>
            </el-table-column>
        </el-table>
        <div class="fl-bw">
            <div></div>
            <el-pagination
                v-model:current-page="currentPage"
                v-model:page-size="pageSize"
                layout="prev, pager, next, jumper"
                v-model:total="total"
                @current-change="handleCurrentChange"
            />
        </div>
    </div>
</template>
<style lang="scss" scoped></style>
